package com.laolei.core.redis;

import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

public class RedisTemplatePackage {
	private StringRedisTemplate stringRedisTemplate;
	private RedisTemplate<String, Object> redisTemplate;
	private IntegerRedisTemplate integerRedisTemplate;
	private LongRedisTemplate longRedisTemplate;
	private EnableTransactionIntegerRedisTemplate enableTransactionIntegerRedisTemplate;
	private ByteRedisTemplate byteRedisTemplate;
	private JsonBytesRedisTemplate jsonBytesRedisTemplate;

	public RedisTemplatePackage(RedisConnectionFactory factory) {
		stringRedisTemplate = stringRedisTemplate(factory);
		redisTemplate = redisTemplate(factory);
		integerRedisTemplate = integerRedisTemplate(factory);
		longRedisTemplate = longRedisTemplate(factory);
		enableTransactionIntegerRedisTemplate = enableTransactionIntegerRedisTemplate(factory);
		byteRedisTemplate = byteRedisTemplate(factory);
		jsonBytesRedisTemplate = jsonBytesRedisTemplate(factory);
	}

	StringRedisTemplate stringRedisTemplate(RedisConnectionFactory factory) {
		StringRedisTemplate template = new StringRedisTemplate(factory);
		template.afterPropertiesSet();
		return template;
	}

	<T> RedisTemplate<String, T> redisTemplate(RedisConnectionFactory factory) {
		RedisTemplate<String, T> template = new RedisTemplate<String, T>();
		template.setConnectionFactory(factory);
		RedisSerializer<String> stringSerializer = new StringRedisSerializer();
		template.setKeySerializer(stringSerializer);
		template.setHashKeySerializer(stringSerializer);
		template.afterPropertiesSet();
		return template;
	}

	IntegerRedisTemplate integerRedisTemplate(RedisConnectionFactory factory) {
		IntegerRedisTemplate template = new IntegerRedisTemplate();
		template.setConnectionFactory(factory);
		template.afterPropertiesSet();
		return template;
	}

	LongRedisTemplate longRedisTemplate(RedisConnectionFactory factory) {
		LongRedisTemplate template = new LongRedisTemplate();
		template.setConnectionFactory(factory);
		template.afterPropertiesSet();
		return template;
	}

	EnableTransactionIntegerRedisTemplate enableTransactionIntegerRedisTemplate(RedisConnectionFactory factory) {
		EnableTransactionIntegerRedisTemplate template = new EnableTransactionIntegerRedisTemplate();
		template.setConnectionFactory(factory);
		template.afterPropertiesSet();
		return template;
	}

	ByteRedisTemplate byteRedisTemplate(RedisConnectionFactory factory) {
		ByteRedisTemplate template = new ByteRedisTemplate();
		template.setConnectionFactory(factory);
		template.afterPropertiesSet();
		return template;
	}

	JsonBytesRedisTemplate jsonBytesRedisTemplate(RedisConnectionFactory factory) {
		JsonBytesRedisTemplate template = new JsonBytesRedisTemplate();
		template.setConnectionFactory(factory);
		template.afterPropertiesSet();
		return template;
	}

	public StringRedisTemplate getStringRedisTemplate() {
		return stringRedisTemplate;
	}

	public RedisTemplate<String, Object> getRedisTemplate() {
		return redisTemplate;
	}

	public IntegerRedisTemplate getIntegerRedisTemplate() {
		return integerRedisTemplate;
	}

	public LongRedisTemplate getLongRedisTemplate() {
		return longRedisTemplate;
	}

	public EnableTransactionIntegerRedisTemplate getEnableTransactionIntegerRedisTemplate() {
		return enableTransactionIntegerRedisTemplate;
	}

	public ByteRedisTemplate getByteRedisTemplate() {
		return byteRedisTemplate;
	}

	public JsonBytesRedisTemplate getJsonBytesRedisTemplate() {
		return jsonBytesRedisTemplate;
	}
}
